package com.zq.zqplayer

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.google.android.material.tabs.TabLayout
import com.ziq.base.base.BaseActivity
import com.zq.zqplayer.databinding.ActivitySplashBinding
import com.zq.zqplayer.mvp.adapter.FragmentViewPagerAdapter
import com.zq.zqplayer.mvp.main.ui.*
import kotlinx.coroutines.*
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.coroutines.CoroutineContext
import com.ziq.base.extension.binding
import com.zq.zqplayer.databinding.ActivityMainBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : BaseActivity() {

    private val binding : ActivityMainBinding by binding()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.root
        initData(savedInstanceState)
    }


    fun initData(savedInstanceState: Bundle?) {
        var iconList: ArrayList<Int> = arrayListOf()
        iconList.add(R.drawable.selector_tab_home)
        iconList.add(R.drawable.selector_tab_amusement)
        iconList.add(R.drawable.selector_tab_subscribe)
        iconList.add(R.drawable.selector_tab_discover)
        iconList.add(R.drawable.selector_tab_mine)

        var dataList: ArrayList<FragmentViewPagerAdapter.Data> = arrayListOf()
        dataList.add(FragmentViewPagerAdapter.Data("首页", HomeFragment()))
        dataList.add(FragmentViewPagerAdapter.Data("娱乐", AmusementFragment()))
        dataList.add(FragmentViewPagerAdapter.Data("订阅", SubscribeFragment()))
        dataList.add(FragmentViewPagerAdapter.Data("发现", DiscoveryFragment()))
        dataList.add(FragmentViewPagerAdapter.Data("我的", MineFragment()))

        var adapter: FragmentViewPagerAdapter = FragmentViewPagerAdapter(dataList, supportFragmentManager)
        binding.viewPager.adapter = adapter;
//        viewPager.offscreenPageLimit = 5
//        mTabLayout.setupWithViewPager(viewPager)
        for(i in 0 until dataList.size){
            val tab : TabLayout.Tab = binding.tabLayout.newTab()
            val tabContent:View = View.inflate(this, R.layout.main_tab_layout, null)
            tabContent.findViewById<ImageView>(R.id.icon).setBackgroundResource(iconList[i])
            tabContent.findViewById<TextView>(R.id.title).text = dataList[i].title
            tab.customView = tabContent
            binding.tabLayout.addTab(tab)
        }
        binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                binding.viewPager.setCurrentItem(tab?.position!!, false)
            }

        })
    }
}
